<?php
namespace App\GraphQL\Mutations\Wallet;

use App\Models\AdminWithdrawOrder;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Support\Facades\Validator;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use App\Jobs\AdminWithdrawJob;

class AdminWithdraw
{

    /**
     * Return a value for the field.
     *
     * @param null $rootValue
     *            Usually contains the result returned from the parent field. In this case, it is always `null`.
     * @param mixed[] $args
     *            The arguments that were passed into the field.
     * @param \Nuwave\Lighthouse\Support\Contracts\GraphQLContext $context
     *            Arbitrary data that is shared between all fields of a single query.
     * @param \GraphQL\Type\Definition\ResolveInfo $resolveInfo
     *            Information about the query itself, such as the execution state, the field name, path to the field from the root, and more.
     * @return mixed
     */
    public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
    {
        DB::transaction(function () use ($args) {
            $data = $args['input'];
            $authUser = auth()->user();
            $data['user_id']=$authUser->id;
            $data['amount'] = intval($data['amount']*100);
            $this->validate($data);
            $data['status'] = 'NORMAL';
            $data['desc'] = '提现';
            
            if($data['channel'] === 'WECHAT'){
                $data['platform_open_id'] = $authUser->wechat_open_id;
            }elseif($data['channel'] === 'ALIPAY'){
                $data['platform_open_id'] = $authUser->alipay_open_id;
            }
            $order = AdminWithdrawOrder::create($data);
            // 锁定个人钱包
            $wallet = $authUser->wallet;
            $wallet->blance = $wallet->blance - $data['amount'];
            $wallet->lock_blance = $wallet->lock_blance + $data['amount'];
            $wallet->save();
            //分发企业付款任务 延时1分钟后执行
            AdminWithdrawJob::dispatch($order)->delay(now()->addMinutes(1));
        });
        return [
            'status' => '200',
            'message' => '申请成功'
        ];
    }

    public function validate($data)
    {
        $channels = [
            'WECHAT',
            'ALIPAY'
        ];
        $rules = [
            'amount'=>[
                'required',
                'numeric',
                'gt:0'
            ],
            'channel'=>[
                'required',
                Rule::in($channels)
            ]
        ];
        $messages = [
            'amount.required' => '名称不能为空',
            'amount.numeric' => '价格为数值',
            'amount.gt' => '提现金额大于0',
            'channel.required' => '提现通道不能为空',
            'amount.gt' => '提现通道不正确',
        ];
        $validate = Validator::make($data, $rules, $messages);
        if ($validate->fails()) {
            throw new \InvalidArgumentException($validate->messages()->first());
        }
        $authUser = auth()->user();
        $wallet = $authUser->wallet;
        if($wallet->blance<$data['amount']){
            throw new \Exception('余额不足!');
        }
        return true;
    }
}
